package com.ericsson.enap.modules.ossalarm.thread;

import com.ericsson.common.util.DateUtils;
import com.ericsson.enap.modules.ossalarm.entity.CnAlarmEntity;
import com.ericsson.enap.modules.ossalarm.mapper.VgcEnmAlarmMapper;
import com.ericsson.enap.modules.sys.entity.EnipNetelement;
import org.slf4j.Logger;

import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 处理ENM告警信息的线程
 * @author yh
 */
public class VgcEnmDealAlarmThread extends Thread{
    private LinkedBlockingQueue msgQueue;
    private Logger log;
    private Map<String, EnipNetelement> netElementMap;
    private VgcEnmAlarmMapper vgcEnmAlarmMapper;
    public VgcEnmDealAlarmThread(VgcEnmAlarmMapper vgcEnmAlarmMapper, Map<String, EnipNetelement> netElementMap, LinkedBlockingQueue e, Logger log){
        this.vgcEnmAlarmMapper=vgcEnmAlarmMapper;
        this.netElementMap=netElementMap;
        this.msgQueue=e;
        this.log=log;
    }
    @Override
    public void run() {
        while(true){
            if(!this.msgQueue.isEmpty()) {
                String msg = null;
                try {
                     msg= this.msgQueue.take().toString();
                     String []msgArr=msg.split("\n");
                     CnAlarmEntity cnAlarmEntity=new CnAlarmEntity();
                     cnAlarmEntity.setAlarmContent(msg);
                     for(String tmp:msgArr){
                         if(tmp.contains("-EventTime")){
                             String eventtime= DateUtils.formattedDateTime(tmp.substring(tmp.indexOf("=")+1,tmp.length()),"yyyyMMddHHmmss", "yyyy-MM-dd HH:mm:ss");
                             cnAlarmEntity.setEventTime(eventtime);
                         }else if(tmp.contains("PerceivedSeverity")){
                             cnAlarmEntity.setAlarmLevel(Long.parseLong(tmp.substring(tmp.indexOf("=")+1,tmp.length())));
                         }else if(tmp.contains("SpecificProblemText")){
                             cnAlarmEntity.setEnglishTitleid(tmp.substring(tmp.indexOf("=")+1,tmp.length()));
                         }else if(tmp.contains("-AlarmId")){
                             cnAlarmEntity.setAlarmId(tmp.substring(tmp.indexOf("=")+1,tmp.length()));
                         }else if(tmp.contains("-Fdn=NetworkElement=")){
                             String netName=tmp.substring(5,tmp.length());
                             EnipNetelement netelement=this.netElementMap.get(netName);
                             cnAlarmEntity.setNetName(netName);
                             cnAlarmEntity.setNetId(netelement.getId());
                             cnAlarmEntity.setNetType(netelement.getType());
                         }
                     }
                    analysisCnAlarm(cnAlarmEntity);
                }catch(Exception ex){
                    log.error("OssDealAlarmThread"+ex.getMessage()+":msg:"+msg);
                }
            }
        }
    }

    /**
     * 分析告警消息，并执行相应的操作
     * @param cnAlarmEntity
     */
    private void analysisCnAlarm(CnAlarmEntity cnAlarmEntity){
        cnAlarmEntity.setCreateTime(DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"));
        //将所有告警信息插入到log表
        this.vgcEnmAlarmMapper.insertVgcEnmAlarmMsgLog(cnAlarmEntity);
        if(cnAlarmEntity.getAlarmLevel().equals(0l)){
            //PerceivedSeverity=0 Indeterminate 不做任何处理
        }else if(cnAlarmEntity.getAlarmLevel().equals(5l)){
            //5级，根据netid和alarmid读取库中的数据状态，
            // 如果为5，将库中的告警数据删除（告警消除）
           this.vgcEnmAlarmMapper.deleteVgcEnmAlarmMsg(cnAlarmEntity.getNetId(),cnAlarmEntity.getAlarmId());
        }else{
            //除0和5外的其它等级告警，根据netid和alarmid读取库中的数据，
            // 如果存在数据，不做任何操作；
            // 插入数据到告警表（enip_enm_original_alarm_data）
            List<CnAlarmEntity> cnAlarmEntities=this.vgcEnmAlarmMapper.queryVgcEnmAlarmMsg(cnAlarmEntity.getNetId(),cnAlarmEntity.getAlarmId());
            if(cnAlarmEntities.size()==0){
                this.vgcEnmAlarmMapper.insertVgcEnmAlarmMsg(cnAlarmEntity);
            }
        }
    }
}
